<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\UserProfile;
use App\Model\WhiteHost;
use App\Model\WhitePhone;
use App\Util\InfoUtil;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

/**
 * 女用户白名单
 */
class WhiteHostController extends Controller
{
    use ModelForm;

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('女用户白名单列表');
            $content->body($this->grid());
        });
    }

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('详情');
            $content->description('');
            $content->body($this->form('edit', $id)->edit($id));
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加白名单');
            $content->description('');
            $content->body($this->form());
        });
    }

    protected function grid()
    {
        return Admin::grid(WhiteHost::class, function (Grid $grid) {
            $grid->model()->orderBy('created_at', 'desc');
            // $grid->id('ID');
            $grid->created_at('添加时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i', strtotime($time));
            });
            $grid->userInfo()->user_profile_id('女用户ID');
            $grid->userInfo()->nick_name('女用户昵称');
            $grid->userInfo()->gender('性别')->display(function ($gender) {
                return $gender == 1 ? '男' : ($gender == 0 ? '女' : '-');
            });
            $grid->flag('状态')->display(function ($data) {
                return $data == 1 ? '生效' : '无效';
            });
            //禁用导出
            $grid->disableExport();
            //禁用行选择
            $grid->disableRowSelector();
            $grid->filter(function ($filter) {
                // $filter->expand();
                $filter->disableIdFilter();
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('userIndo.user_profile_id', '女用户ID');
                });
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                // $actions->disableDelete();
                $actions->disableView();
                $actions->disableEdit();
            });
        });
    }

    /**
     * Make a form builder.
     * @return Form
     */
    protected function form($mode = '', $id = '')
    {
        return Admin::form(WhiteHost::class, function (Form $form) use ($mode, $id) {
            $form->text('user_profile_id', '女用户ID')->required();
            $form->saving(function (Form $form) {
                $profileId = trim($form->user_profile_id);
                $user = UserProfile::where('user_profile_id', $profileId)->first();
                if (!$user || WhiteHost::checkWhite($user->user_id)) {
                    return InfoUtil::adminErr("{$profileId}用户已在白名单，或者信息有误！");
                }
                WhiteHost::addHost($user->user_id);
                return InfoUtil::adminSuc("成功添加白名单！");
            });
            $form->tools(function (Form\Tools $tools) {
                $tools->disableList();
                $tools->disableView();
            });
        });
    }
}
